// pages/recruitEdubackAdd/recruitEdubackAdd.js
var app = getApp();
const util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '',
    formlist: [
      { isrequire: true, label: '学校名称', key: 'schoolName', valueType: 'text', tips: '请填写', type: 'input' },
      {
        isrequire: true, label: '学历/学位', key: 'eduRecord', rangeKey: 'name', valueType: 'text', tips: '请填写', type: 'input'},
      { isrequire: true, label: '入学时间', key: 'enterSchoolDate', valueType: 'text', tips: '请选择', type: 'select', mode: 'date' },
      { isrequire: true, label: '毕业时间', start:'', key: 'outSchoolDate', valueType: 'text', tips: '请选择', type: 'select', mode: 'date' }
    ],
    formData: {
      schoolName: '',
      eduRecord: "",
      enterSchoolDate: '',
      outSchoolDate: ''
    },
    now: '',
    canSubmit: false,
    isEdit:false
  },
  bindPickerChange: function (e) {
    var item = e.currentTarget.dataset.item;
    if (item.data) {
      this.data.formData[item.key] = item.data[e.detail.value];
    } else {
      this.data.formData[item.key] = e.detail.value;
      if (item.key == 'enterSchoolDate') {
        this.setData({
          ['formlist[3].start']: e.detail.value
        })
      }
    }
    this.setData({
      formData: this.data.formData
    })
    this.changeValue();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo')),
      now: util.getNow().substring(0, 7)
    })
    var that=this;
    if (options.isEdit){
      this.setData({
        isEdit:true
      })
      wx.getStorage({
        key: 'infoItem',
        success: function (res) {
          for (var j in that.data.formData) {
            if (res.data[j] !== '' && res.data[j] != undefined && res.data[j] != null) {
              that.data.formData[j] = res.data[j];
            }
          }
          that.data.formData.id = res.data.id;
          that.setData({
            formData: that.data.formData
          })
          that.changeValue();
          wx.removeStorage({
            key: 'infoItem'
          })
        }
      })
    }
  },
  changeTextarea: function (e) {//输入框值改变事件
    var key = e.target.dataset.value;
    this.data.formData[key] = e.detail.value;
    this.setData({
      formData: this.data.formData
    })
    this.changeValue();
  },
  save: function (e) {
    if (this.data.canSubmit) {
        var param = {};
        var url='';
        if (this.data.isEdit){
          url ='recruitEducationInfo/update';
        }else{
          url = 'recruitEducationInfo/save';
          param.userId = this.data.userInfo.id;
        }
        for (var j in this.data.formData) {
          param[j] = this.data.formData[j];
        }
        var that = this;
        wx.request({
          url: app.globalData.httpApiCar + url, //仅为示例，并非真实的接口地址
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          data: param,
          success: function (res) {
            if (res.data.success) {
              wx.navigateBack({
                delta:1
              })
            }
            else {
              wx.showToast({
                title: res.data.error,
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
    }
  },
  del: function (e) {
    wx.request({
      url: app.globalData.httpApiCar + 'recruitEducationInfo/del', //仅为示例，并非真实的接口地址
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: { id: this.data.formData.id },
      success: function (res) {
        if (res.data.success) {
          wx.navigateBack({
            delta: 1,
          })
        }else{
          wx.showModal({
            title: '删除失败',
            showCancel:false,
            content: '',
          })
        }
      }
    })
  },
  changeValue() {
    var flag = true;
    var item = this.data.formData;
          if ((item.schoolName == '') || (item.eduRecord == '') || (item.enterSchoolDate == '') || (item.outSchoolDate == '')) {
      flag = false;
    }
    this.setData({
      canSubmit: flag
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})